<?php

namespace App\Service;

use Illuminate\Support\Facades\Log;

/**
 * 请求服务逻辑.
 */
class RequestService {
	const CODE_STATUS = 1002; //修改状态
	const CODE_ADD_SERVER = 1003; //新增服务器
	const CODE_CHANGE_PORT = 2024; //修改端口
	const CODE_ORDER_PAY = 2001; //支付通知
	const CODE_PLAYER_KICK = 1005; //踢角色下线
	const CODE_ROLE_BAN = 2013; //禁封账号
	const CODE_ROLE_POST = 2012; //角色禁言
	const CODE_ACCOUNT_BAN = 2010; //禁封角色
	const CODE_WELL_ROLE = 1009; //恢复角色
	const CODE_EMAIL = 2015; //发邮件
	const CODE_VIP = 1006; //加vip等级
	const CODE_NOTICE = 1007; //登陆公告
	const CODE_DELETE_ITEM = 2026; //删除玩家物品

	/**
	 * [sendOrderPay 支付成功后通知游戏服].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-02
	 *
	 * @version  1.0
	 *
	 * @param int $server_id  [游戏服id]
	 * @param int $orderId    [订单id]
	 * @param int $platformId [渠道id]
	 * @param int $playerid   [玩家id]
	 * @param int $payid
	 * @param int $issucc
	 *
	 * @return bool
	 */
	public static function sendOrderPay($server_id, $orderId, $platformId, $playerid, $payid, $issucc = 0) {
		$op = $issucc.';' . $server_id . ';' . $orderId . ';1;'
			. $platformId . ';' . $playerid . ';' . $payid;
		$send_data = [
			'cmd' => self::CODE_ORDER_PAY,
			'data' => [
				'server_id' => intval($server_id),
				'op' => $op,
			],
		];
		$ret = self::sendSock($send_data, '', '', 1);
		if (10 == $ret['error']) {
			return true;
		}

		return false;
	}

	/**
	 * [sendNotice 发布公告].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-16
	 *
	 * @version  1.0
	 *
	 * @param int    $server_id
	 * @param string $option
	 *
	 * @return bool
	 */
	public static function sendNotice($server_id, $option) {
		$send_data = [
			'cmd' => self::CODE_NOTICE,
			'data' => [
				'server_id' => intval($server_id),
				'op' => $option,
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [sendVip 加vip等级].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-16
	 *
	 * @version  1.0
	 *
	 * @param string $option
	 * @param int    $server_id
	 *
	 * @return bool
	 */
	public static function sendVip($option, $server_id) {
		$send_data = [
			'cmd' => self::CODE_VIP,
			'data' => [
				'server_id' => intval($server_id),
				'op' => $option,
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [emailAward 邮件奖励].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-12
	 *
	 * @version  1.0
	 *
	 * @param string
	 *
	 * @return bool
	 */
	public static function emailAward($data) {
		$send_data = [
			'cmd' => self::CODE_EMAIL,
			'data' => $data,
		];

		return self::sendSock($send_data);
	}

	/**
	 * [bannedRole 角色禁言].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-10
	 *
	 * @version  1.0
	 *
	 * @param int    $server_id   [游戏服id]
	 * @param string $player_name [角色名]
	 * @param int    $delayTime   [禁言时长]
	 * @param int    $flag        [1禁言，0解除]
	 *
	 * @return bool
	 */
	public static function bannedRole($server_id, $player_name, $delayTime = 0, $flag = 0) {
		$send_data = [
			'cmd' => self::CODE_ROLE_POST,
			'data' => [
				'server_id' => intval($server_id),
				'role_name' => $player_name,
				'time' => intval($delayTime),
				'flag' => $flag,
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [banAccount 禁封账号].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-19
	 *
	 * @version  1.0
	 *
	 * @param int    $platFormId [平台号]
	 * @param string $account    [账号]
	 * @param int    $flag       [状态1禁封,0解封]
	 * @param int    $delayTime  [解封时间]
	 *
	 * @return array|bool
	 */
	public static function banAccount($platFormId, $account, $flag = 0, $delayTime = 0) {
		$send_data = [
			'cmd' => self::CODE_ROLE_BAN,
			'data' => [
				'platFormId' => intval($platFormId),
				'account' => $account,
				'flag' => intval($flag),
				'delayTime' => intval($delayTime),
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [banRole 禁封角色].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-09
	 *
	 * @version  1.0
	 *
	 * @param int $server_id [服务器id]
	 * @param int $role_id   [角色名称]
	 *
	 * @return array|bool
	 */
	public static function banRole($server_id, $role_id) {
		$send_data = [
			'cmd' => self::CODE_ACCOUNT_BAN,
			'data' => [
				'server_id' => intval($server_id),
				'role_id' => intval($role_id),
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [wellRole 恢复角色登录].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-05-09
	 *
	 * @version  1.0
	 *
	 * @param int    $server_id [游戏服id]
	 * @param string $op        [{userid;playerid}]
	 *
	 * @return array|bool
	 */
	public static function wellRole($server_id, $op) {
		$send_data = [
			'cmd' => self::CODE_WELL_ROLE,
			'data' => [
				'server_id' => intval($server_id),
				'op' => $op,
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [playerKickOff 踢角色下线].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-04-08
	 *
	 * @version  1.0
	 *
	 * @param int    $server_id [服务器id]
	 * @param string $role_name [角色名]
	 *
	 * @return array|bool
	 */
	public static function playerKickOff($server_id, $role_name) {
		$send_data = [
			'cmd' => self::CODE_PLAYER_KICK,
			'data' => ['server_id' => intval($server_id), 'role_name' => $role_name],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [changeStatus 修改服务器状态].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-03-08
	 *
	 * @version  1.0
	 *
	 * @param int $server_id [description]
	 * @param int $state     [description]
	 *
	 * @return array|bool
	 */
	public static function changeState($server_id, $state) {
		$send_data = [
			'cmd' => self::CODE_STATUS,
			'data' => ['server_id' => intval($server_id), 'state' => intval($state)],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [sendAddMsg 向游戏服发送开服消息].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-03-08
	 *
	 * @version  1.0
	 *
	 * @return array|bool
	 */
	public static function sendAddMsg($data) {
		$send_data = [
			'cmd' => self::CODE_ADD_SERVER,
			'data' => $data,
		];

		return self::sendSock($send_data);
	}

	/**
	 * [changePort 修改游戏服端口].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-03-08
	 *
	 * @version  1.0
	 *
	 * @param int $server_id [description]
	 * @param int $port_id   [description]
	 *
	 * @return array|bool
	 */
	public static function changePort($server_id, $port_id) {
		$send_data = [
			'cmd' => self::CODE_CHANGE_PORT,
			'data' => ['server_id' => intval($server_id), 'port' => intval($port_id)],
		];

		return self::sendSock($send_data);
	}

	/**
	 * //删除玩家道具.
	 *
	 * @param int $server_id
	 * @param int $role_id
	 * @param int $item_id
	 * @param int $local     1:背包2:仓库3:玩家身上
	 * @param int $is_bind
	 * @param int $number
	 *
	 * @return null|array
	 */
	public static function deleteRoleItem($server_id, $role_id, $item_id, $local, $is_bind, $number) {
		$send_data = [
			'cmd' => self::CODE_DELETE_ITEM,
			'data' => [
				'server_id' => $server_id,
				'role_id' => $role_id,
				'item_id' => $item_id,
				'local' => $local,
				'is_bind' => $is_bind,
				'number' => $number,
			],
		];

		return self::sendSock($send_data);
	}

	/**
	 * [curl curl请求].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-03-08
	 *
	 * @version  1.0
	 *
	 * @param string $url    [发送地址]
	 * @param bool   $params [参数]
	 * @param int    $ispost [是否post]
	 * @param int    $https  [是否是https]
	 *
	 * @return array
	 */
	public static function curl($url, $params = false, $ispost = 0, $https = 0) {
		$httpInfo = [];
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		if ($https) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
		}
		if ($ispost) {
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
			curl_setopt($ch, CURLOPT_URL, $url);
		} else {
			if ($params) {
				if (is_array($params)) {
					$params = http_build_query($params);
				}
				curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
			} else {
				curl_setopt($ch, CURLOPT_URL, $url);
			}
		}

		$response = curl_exec($ch);

		if (false === $response) {
			//echo "cURL Error: " . curl_error($ch);
			return false;
		}
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
		curl_close($ch);

		return $response;
	}

	/**
	 * [sendSock sock方式发送数据].
	 *
	 * @Author   王文凡
	 * @DateTime 2018-03-08
	 *
	 * @version  1.0
	 *
	 * @param string $host   [地址]
	 * @param string $port   [端口]
	 * @param array  $data   [数据]
	 * @param int    $is_ret [是否返回全部数据]
	 *
	 * @return array|bool
	 */
	public static function sendSock($data, $host = '', $port = '', $is_ret = 0) {
		$host = $host ? $host : config('game.erlang.host');
		$port = $port ? $port : config('game.erlang.port');
		Log::info('request to game server socket message :', ['host' => $host, 'port' => $port, 'data' => $data]);
		$fp = @fsockopen($host, $port, $errno, $errstr, 5);
		if (!$fp) {
			Log::error('send socket message fail!');

			return false;
		}
		$json = json_encode($data);
		fwrite($fp, $json, strlen($json));
		$re = fread($fp, 8192);
		fclose($fp);
		$result = json_decode($re, true);
		Log::info('response form game server socket message :', ['ret' => $result]);
		if ($is_ret) {
			return $result;
		}
		if (!isset($result['error']) || 0 != $result['error']) {
			return false;
		} else {
			return true;
		}
	}
}
